package com.laboratory.controller;

import com.laboratory.domain.ResponseCode;
import com.laboratory.domain.ResponseResult;
import com.laboratory.domain.dto.FormDto;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/forms")
public class FormController {

    /**
     * JSON 提交：触发 MethodArgumentNotValidException / ConstraintViolation
     */
    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult createByJson(@Validated @RequestBody FormDto form) {
        return new ResponseResult(
                ResponseCode.GET_SUCCESS,
                "JSON 提交校验通过",
                form
        );
    }

    /**
     * 表单 / Query 提交：触发 BindException（容器元素同样会校验）
     */
    @PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public ResponseResult createByForm(@Validated @ModelAttribute FormDto form) {
        return new ResponseResult(
                ResponseCode.GET_SUCCESS,
                "表单提交校验通过",
                form
        );
    }

}
